﻿package mortal.game.manager
{
    import com.gengine.global.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.ui.*;

    public class CursorManagerImpl extends Sprite
    {
        private static var _cursorStyle:String;
        private static var _currentContainer:GBitmap;
        private static var _instance:CursorManagerImpl;

        public function CursorManagerImpl()
        {
            if (_instance != null)
            {
                throw new Error(" CursorManagerImpl 单例 ");
            }
            return;
        }// end function

        public function dispose() : void
        {
            return;
        }// end function

        public function addFilters(param1:Array) : void
        {
            if (_cursorStyle != CursorManager.Arrow)
            {
                if (_currentContainer)
                {
                    _currentContainer.filters = param1;
                }
            }
            return;
        }// end function

        public function clearFilters() : void
        {
            if (_currentContainer)
            {
                _currentContainer.filters = null;
            }
            return;
        }// end function

        public static function get instance() : CursorManagerImpl
        {
            if (_instance == null)
            {
                _instance = new CursorManagerImpl;
                _instance.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
            }
            return _instance;
        }// end function

        private static function addedToStageHandler(event:Event) : void
        {
            _instance.mouseChildren = false;
            _instance.mouseEnabled = false;
            cursorStyle = CursorManager.Arrow;
            return;
        }// end function

        private static function onMouseMoveHandler(event:Event) : void
        {
            var _loc_2:Point = null;
            if (_currentContainer && _instance.contains(_currentContainer))
            {
                _loc_2 = _instance.localToGlobal(new Point(0, 0));
                _currentContainer.x = _loc_2.x + _instance.mouseX;
                _currentContainer.y = _loc_2.y + _instance.mouseY;
            }
            return;
        }// end function

        public static function get cursorStyle() : String
        {
            return _cursorStyle;
        }// end function

        public static function set cursorStyle(param1:String) : void
        {
            _cursorStyle = param1;
            if (_cursorStyle == CursorManager.Arrow)
            {
                if (_currentContainer && _instance.contains(_currentContainer))
                {
                    _currentContainer.dispose(true);
                    _currentContainer = null;
                }
                _instance.clearFilters();
                Mouse.show();
                Global.stage.removeEventListener(Event.ENTER_FRAME, onMouseMoveHandler);
            }
            else
            {
                if (_currentContainer)
                {
                    _currentContainer.bitmapData = GlobalClass.getBitmapData(param1);
                }
                else
                {
                    _currentContainer = GlobalClass.getBitmap(param1);
                }
                if (_instance.contains(_currentContainer) == false)
                {
                    _instance.addChild(_currentContainer);
                }
                _currentContainer.x = _instance.mouseX;
                _currentContainer.y = _instance.mouseY;
                Global.stage.addEventListener(Event.ENTER_FRAME, onMouseMoveHandler);
            }
            return;
        }// end function

    }
}
